/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SumGenerator.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 3, 2009  Raghu Sarraff	 		        Created
 */
package net.lwf.handler.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;

import lwf.handler.entities.math.MadMinSum;
import lwf.handler.entities.math.Sum;
import lwf.handler.entities.math.SumInput;

public class SumGenerator {
	
	public static List<MadMinSum> generate2(SumInput input) {
		List<MadMinSum> allsums = new ArrayList<MadMinSum>();
		for(int i=0; i<input.getRequiredpages(); i++) {
			//List rowsperpage = new ArrayList(input.getRowsperpage());
			for(int j=0; j<input.getRowsperpage(); j++) {
				for(int m=0; m<input.getSumsperrow(); m++) {
					String operation = input.getOperation();
					MadMinSum sum = new MadMinSum(operation, (j+1), (m+1), input.getDecimalpoints());
					if(m == (input.getSumsperrow()-1)) {
						sum.setEndRow(true);
					}
					//Set rownumber and sumnumber
					int datarows = input.getDatarowsinsum();
					if(!operation.equals(IHandlerConstant.ADDITION)) {
						datarows = 2;
					}
					List<Double> sumdata = new ArrayList<Double>(datarows);
					String datast = null;
					for(int k=0; k<datarows; k++) {
						double data = 0;
						if( k != 0 && input.getDigits() > 1 && (sum.getOperation().equals(IHandlerConstant.MULTIPLICATION) || sum.getOperation().equals(IHandlerConstant.DIVISION))) {
							data = RandomNumberGenerator.generateDouble(input.getDigits()-1, input.getDecimalpoints());
						}
						else {
							data = RandomNumberGenerator.generateDouble(input.getDigits(), input.getDecimalpoints());
							datast += String.valueOf(data) + " AND "; //????do not know why this????
						}
						sumdata.add(new Double(data));
					}
					
					Collections.sort(sumdata); //Sorts ascending order
					sum.setData(sumdata);
					allsums.add(sum);
				}
			}
		}
		return allsums;
	}
	
	public static Sum generateAdd(int digits, int datarows, int decimalpoints) {
		return generate(IHandlerConstant.ADDITION, digits, datarows, decimalpoints);
	}

	public static Sum generateSubtract(int digits, int decimalpoints) {
		return generate(IHandlerConstant.SUBTRACTION, digits, 2, decimalpoints);
	}
	
	public static Sum generateMultiply(int digits, int decimalpoints) {
		return generate(IHandlerConstant.MULTIPLICATION, digits, 2, decimalpoints);
	}
	
	public static Sum generateDevide(int digits, int decimalpoints) {
		return generate(IHandlerConstant.DIVISION, digits, 2, decimalpoints);
	}
	
	private static Sum generate(String operation, int digits, int datarows, int decimalpoints) {
		Sum sum = new Sum(operation, decimalpoints);
		List<Double> sumdata = new ArrayList<Double>(datarows);
		for(int k=0; k<datarows; k++) {
			double data = 0;
			if(operation.equals(IHandlerConstant.MULTIPLICATION)) {
				data = RandomNumberGenerator.generateDouble(digits-1, decimalpoints);
			}
			if(operation.equals(IHandlerConstant.DIVISION)) {
				while(data == 0) {
					data = RandomNumberGenerator.generateDouble(digits, decimalpoints);
				}
			}
			else {
				data = RandomNumberGenerator.generateDouble(digits, decimalpoints);
			}

			sumdata.add(new Double(data));
		}
		
		Collections.sort(sumdata); //Sorts ascending order
		sum.setData(sumdata);
		//sum.print();
		return sum;
	}
	
	public static void main(String[] args) {
		//Test Random
		//test(null);
		//Test Add
		//test(IHandlerConstant.ADDITION);
		//Test Subtract
		//test(IHandlerConstant.SUBTRACTION);
		//Test multiply
		//test(IHandlerConstant.MULTIPLICATION);
		//Test Devide
		//test(IHandlerConstant.DIVISION);
		
		//New code
		testNew();
	}

	/**
	 * 
	 */
	private static void test(String operation) {
		System.out.println("^^^^^^^^^^^^^ Operation: " + operation);
		SumInput input = new SumInput(operation); //Random
		input.setRequiredsums(2);
		input.setDigits(3);
		input.setDatarowsinsum(2); //applicable only for addition
		//for(int k=0; k< input.getRequiredsums(); k++) {
			input.setDecimalpoints(1);
			List<MadMinSum> sumdata = generate2(input);
			for(int i=0; i<sumdata.size(); i++) {
				Sum sum = (Sum) sumdata.get(i);
				sum.print();
			}			
			System.out.println("_________________________________________________________________________________________");
		//}
		System.out.println("^^^^^^^^^^^^^ End of Test for Operation: " + operation);
	}
	
	private static void testNew() {
		int digits = 1;
		int decimalpoints = 0;
		//For add
		Sum sum = generateAdd(digits, 2, decimalpoints);
		sum.print();
		
		//For Subtract
		sum = generateSubtract(digits, decimalpoints);
		sum.print();
		
/*		//For Multiply
		sum = generateMultiply(digits, decimalpoints);
		sum.print();
		
		//For Devide
		sum = generateDevide(digits, decimalpoints);
		sum.print();*/
	}
}

